package com.example.springbott.service;

import com.example.springbott.bean.MarketCoupon;
import com.example.springbott.bean.MarketCouponExample;
import com.example.springbott.bean.MarketCouponUser;
import com.example.springbott.bean.MarketTopicExample;
import com.example.springbott.bo.CreateCouponBo;
import com.example.springbott.bo.UpdateCouponBo;
import com.example.springbott.utils.CodeCreate;
import com.example.springbott.vo.CreateCouponVo;
import com.example.springbott.vo.MarketCouponVo;
import com.example.springbott.common.CommonData;
import com.example.springbott.mapper.MarketCouponMapper;
import com.example.springbott.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 付常莹
 * @description: TODO
 * @date 2022/11/20 13:47
 * name,type,status
 */
@Service
@EnableTransactionManagement
public class MarketCpServiceIml implements MarketCpService{
    @Resource
    MarketCouponMapper marketCouponMapper;


    /**
     * 查找，显示优惠券
     * @param page
     * @param limit
     * @param name
     * @param type
     * @param status
     * @param sort
     * @param order
     * @return
     */
    @Override
    public CommonData<MarketCoupon> selectCoupon(Integer page, Short limit, String name, Short type, Short status, String sort, String order) {
        PageHelper.startPage(page,limit);
        MarketCouponExample couponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = couponExample.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        if (type!=null){
            criteria.andTypeEqualTo(type);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        List<MarketCoupon> list = marketCouponMapper.selectByExample(couponExample);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<MarketCoupon>(list);
        int page1 = pageInfo.getPageNum();
        int pages = pageInfo.getPages();
        int limit1 = pageInfo.getPageSize();
        long total = pageInfo.getTotal();
        List<MarketCoupon> list1 = pageInfo.getList();
        CommonData<MarketCoupon> data = new CommonData<>(page1, pages,(int) total, limit1,  list1);
        return data;
    }




    /**
     * 新增优惠券
     * @param marketCoupon
     * @return
     */
    @Override
    public MarketCoupon createCoupon(MarketCoupon marketCoupon) {
        long l = System.currentTimeMillis();
        Date date = new Date(l);
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        if (marketCoupon.getType()==2){
          String code = CodeCreate.getCode();
          marketCoupon.setCode(code);
        }
        marketCouponMapper.insertSelective(marketCoupon);
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
        return marketCoupon1;
    }



    /**删除优惠券
     *
     * @param marketCoupon
     */
    @Override
    public void deleteCoupon(MarketCoupon marketCoupon) {
        marketCouponMapper.deleteByPrimaryKey(marketCoupon.getId());
    }


    /**
     * 修改优惠券
     * @param marketCoupon
     * @return
     */
    @Override
    public MarketCoupon updateCoupon(MarketCoupon marketCoupon) {
       marketCouponMapper.updateByPrimaryKeySelective(marketCoupon);
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(marketCoupon.getId());
        return marketCoupon1;

    }

    @Override
    public MarketCoupon readCoupon(Integer id) {
        MarketCoupon marketCoupon1 = marketCouponMapper.selectByPrimaryKey(id);
        return marketCoupon1;
    }


}
